package com.zjjc.QZEnergy.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjjc.QZEnergy.domain.HeatData;
import com.zjjc.QZEnergy.domain.Response;
import com.zjjc.QZEnergy.mapper.HeatDataMapper;
import com.zjjc.QZEnergy.service.IDataProcessService;
import com.zjjc.QZEnergy.utils.CryptoUtils;
import com.zjjc.QZEnergy.utils.DateUtils;
import com.zjjc.QZEnergy.utils.HttpsPostUtil;
import com.zjjc.QZEnergy.vo.ReportData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;

@Component
public class QuartzJob {

    @Autowired
    private IDataProcessService dataProcessService;


    @Value("${config.appId}")
    private  String appId;

    @Value("${config.authUrl}")
    private  String authUrl;

    @Value("${config.dataUrl}")
    private  String uploadUrl;

    @Value("${config.privateKeyIsv}")
    private  String privateKey;

    private static final String TYPE_STEAM = "steam";
    private static final String TYPE_GAS = "gas";
    private static final String TYPE_DAY = "day";
    private static final String TYPE_MONTH = "month";

    @Scheduled(cron = "0 5 0 * * ?") //每天00:05上传日度
    public void dailyHeatUpload() throws Exception {
        ResponseEntity<String> authResponse = HttpsPostUtil.sendAuthPostRequest(appId, authUrl);//发送认证请求
        Response resp = new ObjectMapper().readValue(authResponse.getBody().toString(), Response.class);
        String SM4str = CryptoUtils.sm2Decrypt(privateKey, resp.getData().get("appKey").toString());//明文SM4信息，前32字符为hex格式SM4秘钥，后32字符为hex格式IV向量

        Calendar start = DateUtils.getFirstSecondOfLastDay();
        Calendar end = DateUtils.getLastSecondOfLastDay();
        ArrayList<ReportData> list = dataProcessService.dailyDataSelect(start.getTime(), end.getTime(), TYPE_STEAM, TYPE_DAY);
        ResponseEntity<String> response = HttpsPostUtil.sendDataPostRequest(appId, list, uploadUrl,SM4str);
        dataProcessService.insertRecords(list,response.toString(),uploadUrl,appId);
    }

    @Scheduled(cron = "0 0 9 1 * ?") //每月1号9点上传月度数据
    public void monthlyHeatUpload() throws Exception {
        ResponseEntity<String> authResponse = HttpsPostUtil.sendAuthPostRequest(appId, authUrl);//发送认证请求
        Response resp = new ObjectMapper().readValue(authResponse.getBody().toString(), Response.class);
        String SM4str = CryptoUtils.sm2Decrypt(privateKey, resp.getData().get("appKey").toString());//明文SM4信息，前32字符为hex格式SM4秘钥，后32字符为hex格式IV向量

        Calendar start = DateUtils.getFirstDayOfLastMonth();
        Calendar end = DateUtils.getLastDayOfLastMonth();
        ArrayList<ReportData> list = dataProcessService.monthlyDataSelect(start.getTime(), end.getTime(), TYPE_STEAM, TYPE_MONTH);
        ResponseEntity<String> response = HttpsPostUtil.sendDataPostRequest(appId, list, uploadUrl,SM4str);
        dataProcessService.insertRecords(list,response.toString(),uploadUrl,appId);
    }
}
